//
//  DepositsViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/17.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "DepositsViewController.h"

#import "DepositsAmountCell.h"
#import "DepositsUserAuthCell.h"
#import "DepositsBankInfoCell.h"

@interface DepositsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableDictionary *deposParams;

@end

@implementation DepositsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topTitle = @"提现";
    self.topNavView.backgroundColor = MainBackColor;
    self.view.backgroundColor = MainBackColor;
    [self initUI];
    [self initData];
}
#pragma mark - data
-(void)initData{
    self.deposParams = [NSMutableDictionary dictionary];
    self.deposParams[@"amount"] = @"";
    self.deposParams[@"name"] = @"";
    self.deposParams[@"idcard"] = @"";
    self.deposParams[@"account"] = @"";
    self.deposParams[@"cardno"] = @"";
    self.deposParams[@"bank"] = @"";
    self.deposParams[@"openbank"] = @"";
    [self.tableView reloadData];
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.tableView];
}
#pragma mark - lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topNavView.bottom, ScreenWidth, ScreenHeight - self.topNavView.height - kSafe_Bottom) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = MainBackColor;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
        [_tableView setTableHeaderView:view];
        [_tableView setTableFooterView:[self tableFooterView]];
    }
    return _tableView;
}

-(UIView *)tableFooterView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(50, 26, ScreenWidth-100, 40);
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = 20;
    btn.backgroundColor = MainThemeColor;
    [btn.titleLabel setFont:FontSet(17)];
    [btn setTitle:@"确认提现" forState:normal];
    [btn setTitleColor:UIColor.whiteColor forState:normal];
    [btn addTarget:self action:@selector(deposClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    return view;
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 184;
    }else if (indexPath.row == 1) {
        return 194;
    }else{
        return 304;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DepositsAmountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DepositsAmountCell"];
        if (!cell) {
            cell = [[DepositsAmountCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DepositsAmountCell"];
        }
        cell.deposDict = self.deposParams;
        [cell setUpdateDeposBlock:^(NSMutableDictionary * _Nonnull dict) {
            self.deposParams = dict;
            [self.tableView reloadData];
        }];
        return cell;
    }else if (indexPath.row == 1) {
        DepositsUserAuthCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DepositsUserAuthCell"];
        if (!cell) {
            cell = [[DepositsUserAuthCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DepositsUserAuthCell"];
        }
        cell.deposDict = self.deposParams;
        [cell setUpdateDeposBlock:^(NSMutableDictionary * _Nonnull dict) {
            self.deposParams = dict;
            [self.tableView reloadData];
        }];
        return cell;
    }else{
        DepositsBankInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DepositsBankInfoCell"];
        if (!cell) {
            cell = [[DepositsBankInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DepositsBankInfoCell"];
        }
        cell.deposDict = self.deposParams;
        [cell setUpdateDeposBlock:^(NSMutableDictionary * _Nonnull dict) {
            self.deposParams = dict;
            [self.tableView reloadData];
        }];
        return cell;
    }
}
#pragma mark - SEL
-(void)deposClick{
    NSString *amount = self.deposParams[@"amount"];
    if ([amount isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请输入提现金额"];
        return;
    }
    if ([amount floatValue] > [UserInfomationManager shareManager].balance) {
        [[XNProgressHUD shared] showWithTitle:@"可提现余额不足"];
        return;
    }
    NSString *name = self.deposParams[@"name"];
    if ([name isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请输入联系人姓名"];
        return;
    }
    NSString *idcard = self.deposParams[@"idcard"];
    if ([idcard isEmpty] == YES || idcard.length != 18) {
        [[XNProgressHUD shared] showWithTitle:@"请输入正确的身份证号"];
        return;
    }
    NSString *account = self.deposParams[@"account"];
    if ([account isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请输入银行账户"];
        return;
    }
    NSString *cardno = self.deposParams[@"cardno"];
    if ([cardno isEmpty] == YES || cardno.length <= 15 || cardno.length >= 20) {
        [[XNProgressHUD shared] showWithTitle:@"请输入正确的银行卡卡号"];
        return;
    }
    NSString *openbank = self.deposParams[@"openbank"];
    if ([openbank isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请输入开户银行"];
        return;
    }
    NSString *bank = self.deposParams[@"bank"];
    if ([bank isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请输入开户银行地址"];
        return;
    }
    
    [NetWorkingPerson userWithDrawWithParams:self.deposParams WithSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] showSuccessWithTitle:@"提现成功！提现金额将在24小时之内到您的账户！"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }];
}


@end
